import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../model.dart';
import '../req.dart';


//返回首页
Widget homet( Future<List<Album>> futureAlbum ){

  //首页
  Widget home =  FutureBuilder<List<Album>>(
      future: futureAlbum,
      builder: (context, snapshot) {

        if (snapshot.hasData) {

          if(snapshot.data.length == 0){
            return Center(
              child: Text("暂无信件...."),
            );
          }
          return ListView.builder(
            //列表数量
            itemCount: snapshot.data.length,
            //动态生成
            itemBuilder: (BuildContext context, int index){
              return Dismissible(
                onDismissed: (direction){


                  deleteAlbum(snapshot.data[index].id);
                  print("删除");

                },
                key: Key(UniqueKey().toString()),
                child:messageWidget(snapshot.data[index]) ,
              );
             // return ;
            },
          );

        } else if (snapshot.hasError) {
          return Text("${snapshot.error}");
        }

        // By default, show a loading spinner.
        return Center(child: CircularProgressIndicator());
      },
    );



  return home;
}


//更新方法


//返回一个消息Widget
Widget messageWidget(Album al){


  Widget title = Container(

    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        //第一行内容
        Row(
            children: [
              Container(
                margin: const EdgeInsets.only(left: 8),
              ),
              Text('发件人:'+al.send),
              Container(
                margin: const EdgeInsets.only(left: 8),
              ),
              Text('接收人:'+al.form),

            ]
        ),

        Row(
          children: [
            Text('时间:'+al.time),
            Container(
              margin: const EdgeInsets.only(right: 8),
            ),
          ],
        ),
      ],
    ),
  );


  Widget amessage = Container(

    margin: const EdgeInsets.only(top: 25),
    decoration: BoxDecoration(

        border: Border(
          // 四个值 top right bottom left
            bottom: BorderSide(
              // 设置单侧边框的样式
                color: Color(0xffEDEDED),
                width: 1,
                style: BorderStyle.solid))),

    child: Column(
      children: [
        title,
        Container(
          margin: const EdgeInsets.only(left: 20, right: 20, top: 11),
          child: Text(
            al.message,
            style: TextStyle(
              fontSize: 18,
            ),

          ),
        ),

        Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [

            Container(
                margin: const EdgeInsets.only(right: 20, top: 5, bottom: 5),
                child: Text(
                  '更多',
                  style: TextStyle(
                    color: Colors.blue,
                  ),
                )
            ),

          ],
        ),

      ],
    ),

  );


  return  amessage;
}